package com.wps.wdztv6.controller.openapi;

import com.wps.wdztv6.api.response.*;
import com.wps.wdztv6.common.R;
import com.wps.wdztv6.config.AppServersProperties;
import com.wps.wdztv6.config.WdztAppInfoConfig;
import com.wps.wdztv6.service.WdztService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/open/api")
public class EditAndPreviewApiController {
    static String[] PREVIEW_MODE = {"high_definition", "ordinary", "cache"}; // 高清预览、普通预览 缓存预览
    @Autowired
    private WdztService wdztService;

    /**
     * 第一步
     * 获取文档在线链接
     * 拓展用户token  WdztAppInfoConfig.USER_TOKEN_KEY  必输项
     */
    @GetMapping("/edit/link")
    public R getDocumentOnlineUrl(
            @RequestParam("file_id") String fileId,
            @RequestHeader(value = WdztAppInfoConfig.USER_TOKEN_KEY, required = true) String token
    ) {

        return wdztService.getEditUrl(fileId, token);
    }

    @PostMapping("/preview/webhook")
    public void previewPreloadWebhook(@RequestBody PreviewPreloadWebhook body) {
        log.info("文档预览欲缓存回调通知:{}", body);
    }

    /**
     * 获取文档在线链接
     */
    @GetMapping("/preview/link")
    public R getDocumentPreviewOnlineUrl(
            @RequestParam("file_id") String fileId,
            @RequestParam(value = "mode", defaultValue = "high_definition") String previewMode,
            @RequestHeader(value = WdztAppInfoConfig.USER_TOKEN_KEY, required = true) String token
    ) {


        boolean checkResult = false;
        for (String s : PREVIEW_MODE) {
            if (s.equals(previewMode)) {
                checkResult = true;
                break;
            }
        }
        if (!checkResult) {
            return R.failed("预览模式不支持,支持的选项有：" + Arrays.toString(PREVIEW_MODE));
        }
        return wdztService.getPreviewUrl(fileId, previewMode, token);
    }

    /**
     * 预览欲加载
     */
    @GetMapping("/edit/online/cooperators")
    public R<EditCooperatorsResponse> getOnlineCooperators(@RequestParam("file_id") String fileId) {
        return wdztService.getEditOnlineCooperators(fileId);
    }

    /**
     * 预览欲加载
     */
    @GetMapping("/preview/preload")
    public R<PreviewPreloadResponse> getDocumentPreviewPreload(@RequestParam("file_id") String fileId,
                                                               @RequestParam("version") Integer version) {
        if (version <= 0) {
            return R.failed("文件版本不能为空");
        }
        // 这个地址就是本Controller生成的
        return wdztService.previewPreload(fileId, version, AppServersProperties.myserverUrl + "/open/api/preview/webhook");
    }

    /**
     * @param fileId
     * @return
     */
    @GetMapping("/edit/refresh")
    public R<EditForceRefreshResponse> forceRefreshEdit(@RequestParam("file_id") String fileId) {
        return wdztService.forceFreshEditLink(fileId);
    }

    @GetMapping("/edit/save")
    public R<EditActiveSaveResponse> activeSaveEdit(@RequestParam("file_id") String fileId) {
        return wdztService.activeSaveEdit(fileId, "30s", false);
    }
}
